<?php

class Ead_CursoController extends App_Controller_Action
{
    public $dir = 'public/upload/curso/';
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/curso.js');
        
        $search = $this->_getParam('inp-search', '');
        $this->view->search = $search;
        $where = 'CUR_ID > 0';//'CUR_STA = "1"';
        if(strlen($search) > 1){
            $where .= ' AND CUR_NOM like "%'.$search.'%"';
        }
               
        $obj = new Ead_Model_DbTable_Curso();
        $result = $obj->fetchAll($where);
        $this->view->result = $this->paginacao($result);
 
    }
    
    
    public function editAction() {
       
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        //$this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ckeditor/ckeditor.js');
        //$option = array('width'=>'70%');
        //echo $this->view->CkeditorHelper('CUR_APR',$option);
        //echo $this->view->CkeditorHelper('CUR_DSC',$option);
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('imagem','library');
        
        $form = new Ead_Form_Curso();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($form->isValid($formData)) {
                    $id = (int) $form->getValue('CUR_ID');

                    //$returnImg = imagem::upload('CUR_FILE',$this->dir,$id);
                    $returnImg2 = imagem::upload('CUR_FILE2',$this->dir,$id.'_p');
                    $returnImg3 = imagem::upload('CUR_FILE3',$this->dir,$id.'_iconp');
                    
                    $data = $form->getValues();
                    
                    $data['CUR_VLR']        = tools::money_to_float($form->getValue('CUR_VLR'));
                    $data['CUR_PROM']       = tools::money_to_float($form->getValue('CUR_PROM'));
                    $data['CUR_PROMDAT']    = Datetools::dma_amd($form->getValue('CUR_PROMDAT'));
                    $data['CUR_STAPROM']    = $form->getValue('CUR_STAPROM');
                    $data['CUR_VLD']        = Datetools::dma_amd($form->getValue('CUR_VLD'));
                    $data['CUR_VES']        = $form->getValue('CUR_VES');
                    $data['CUR_POR']        = tools::money_to_float($form->getValue('CUR_POR'));
                    
                    unset($data['FOR_ID']);
                    unset($data['CUR_FILE2']);
                    unset($data['CUR_FILE3']);
                    unset($data['img2']);
                    unset($data['img3']);
                    

                    //$arr = $data['CTC_IDLST'];
                    unset($data['CTC_IDLST']);
                    //foreach($arr as $cat) $data['CTC_IDLST'] .= $cat.',';
                    
                    $obj = new Ead_Model_DbTable_Curso();

                    if ($id == 0){
                        $obj->insert($data);
                        $id =  $obj->getAdapter()->lastInsertId();                    
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                        imagem::rename($id.'_p',$this->dir,$returnImg2['filenew']);
                        imagem::rename($id.'_iconp',$this->dir,$returnImg3['filenew']);
                    }
                    else
                        $obj->update($data, 'CUR_ID = ' . $id);

                    /*
                    if(strlen($_FILES['CUR_FILE']['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '300', '250');
                    }  
                    */
                    if(strlen($_FILES['CUR_FILE2']['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'_p'.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '270', '120');
                    }
                    
                    if(strlen($_FILES['CUR_FILE3']['tmp_name']) > 1){
                        
                        $img = glob($this->dir.$id.'_iconp'.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '59', '59');
                        
                        $img = glob($this->dir.$id.'_iconp'.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        $filetemp = str_replace('_iconp', '_icond',$filename);
                        imagem::reizeFull($filename,$filetemp, '42', '42');
                    }


                    $obj = new Ead_Model_DbTable_Cursoforma();
                    $obj->delete('CUR_ID = '.(int)$id);
                    foreach($form->getValue('FOR_ID') as $for)
                    {   
                        $data = array(
                            'CUR_ID'    => $id,
                            'FOR_ID'    => $for,
                        );
                        $obj->insert($data);
                    }
                    
                    $obj = new Ead_Model_DbTable_Cursocat();
                    $obj->delete('CUR_ID = '.(int)$id);
                    foreach($form->getValue('CTC_IDLST') as $cat)
                    {   
                        $data = array(
                            'CUR_ID'    => $id,
                            'CTC_ID'    => $cat
                        );
                        $obj->insert($data);
                    }
                    

                    $this->_helper->redirector('index');
                } else {
                    $form->populate($formData);
                }
            } catch(Exception $ex) {
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $form->populate($formData);
            }    
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Ead_Model_DbTable_Curso();
                $row = $obj->fetchRow('CUR_ID = ' . $id)->toArray();
                
                Zend_Loader::loadClass('tools','library');
                $row['CUR_VLR'] = tools::float_to_money($row['CUR_VLR']);
                $row['CUR_POR'] = tools::float_to_money($row['CUR_POR']);
                $row['CUR_PROM'] = tools::float_to_money($row['CUR_PROM']);
                $row['CUR_VLD'] = Datetools::amd_dma($row['CUR_VLD']);
                $row['CUR_PROMDAT'] = Datetools::amd_dma($row['CUR_PROMDAT']);                
                $form->populate($row);
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Ead_Model_DbTable_Curso();
            //$ok = (bool) $obj->delete('CUR_ID = '.(int)$id);
            $data = array('CUR_STA' => 0);
            $ok = (bool) $obj->update($data, 'CUR_ID = ' . $id);
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

